package com.gykfbxxh.imovie;

import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.multidex.MultiDexApplication;

import com.app.utils.util.AppUtils;
import com.baidu.mobstat.StatService;
import com.gykfbxxh.imovie.config.AppConfigTools;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.manager.AppConfigManager;
import com.gykfbxxh.imovie.manager.IPushManager;
import com.gykfbxxh.imovie.utils.ChannelUtil;
import com.gykfbxxh.imovie.utils.NetUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.List;

/**
 * 程序自定义Applicaiton
 * Created by xuxinghai
 * On 2017/8/17
 * 这个application用于分包
 */

public class AppApplication extends MultiDexApplication {

    private static AppApplication mAppApplication;//唯一实例

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getProcessName(this);
        if (!"com.gykfbxxh.imovie".equals(processName)) {
            return;
        }
        mAppApplication = this;
        //设置程序的异常处理器
        initCatchExcep();
        //初始化工具库
        initAppUitls();
        //初始化统计配置
        initTongji();
        //初始化用户配置
        initUserConfig();
        //初始化友盟登陆分享
        initUmeng();
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1){
            //设置是否改变通知栏的颜色
            AppConfigTools.isSetNotify = true;
        }

    }

    /**
     * 初始化用户配置
     */
    private void initUserConfig() {
        // 初始化用户配置
            SharedPreferences sp = AppConfigManager.getInstance().getConfigSP();
            AppConfigTools.isUseWifi = sp.getBoolean(ConfigKey.option_isWifi,false);
            AppConfigTools.netType = NetUtils.getNetworkType();
    }


    /**
     * 配置统计
     */
    private void initTongji(){
        String channel = ChannelUtil.getChannel(this, "no_channel");//获取渠道,默认为test
        StatService.setAppChannel(this, channel, true);// 初始化百度统计渠道
        StatService.setDebugOn(BuildConfig.LOG_DEBUG);

        MobclickAgent.startWithConfigure(new MobclickAgent.UMAnalyticsConfig(this,
                "57753cb067e58e8ec3005876", channel, MobclickAgent.EScenarioType.E_UM_NORMAL));
        MobclickAgent.setDebugMode(BuildConfig.LOG_DEBUG);// 初始化友盟统计渠道

//        //友盟推送渠道
        IPushManager.getInstance().setPushChannel(channel);

    }

    /**
     * 初始化工具类
     */
    private void initAppUitls(){
        AppUtils.getInstance().setContext(mAppApplication);
        AppUtils.getInstance().setmDebug(BuildConfig.LOG_DEBUG);
    }

    /**
     * 注册全局异常
     */
    private void initCatchExcep() {
//        if(BuildConfig.LOG_DEBUG){
//            return;
//        }
//        // 设置该CrashHandler为程序的默认处理器
//        AppExceptionHandler catchExcep = new AppExceptionHandler(this);
//        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
    }

    /**
     * 获取唯一实例
     * @return
     */
    public static AppApplication getInstance() {
        return mAppApplication;
    }

    /**
     * 获取当前进程的name
     *
     * @param context
     * @return
     */
    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }


    /**
     * 初始化友盟登陆分享
     *          的分享和第三方登录
     */
    private void initUmeng() {
        UMShareAPI.get(this);
        //LogCat中观察友盟日志
        com.umeng.socialize.utils.Log.LOG = BuildConfig.LOG_DEBUG ;
        //关闭分享编辑器
        com.umeng.socialize.Config.OpenEditor = false;
        //微信 appid appsecret
        PlatformConfig.setWeixin(AppConfigTools.WECHAT_ID, AppConfigTools.WECHAT_SECRET);
        PlatformConfig.setQQZone(AppConfigTools.QQ_APPID, AppConfigTools.QQ_APPKEY);
        PlatformConfig.setSinaWeibo(AppConfigTools.SINA_APPID, AppConfigTools.SINA_APPKEY , "http://sns.whalecloud.com");
    }



}
